package com.dzx.currentency.commonUnSafe;

import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

@Slf4j
public class ContainExample {

    //请求次数
    private static int clientTotal = 5000;

    //允许同时运行的线程数
    private static int threadTotal = 200;

    private static List<Integer> list = new ArrayList<Integer>();
    private static Vector<Integer> vector =new Vector<>();
    static {
        vector.add(1);
        vector.add(2);
        vector.add(3);
    }
    private  static  List<Integer> safeList  = Collections.synchronizedList(new ArrayList<Integer>());

    private static Set<Integer> set =new HashSet<Integer>();
    private  static  Set<Integer> safeSet  = Collections.synchronizedSet(new HashSet<Integer>());

    private static  Map<Integer,Integer> map = new HashMap<Integer, Integer>();
    private static Map<Integer,Integer> safeMap = new Hashtable<>();
    private  static  Map<Integer,Integer> safeMap1  = Collections.synchronizedMap(new HashMap<Integer,Integer>());

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();
        final Semaphore semaphore = new Semaphore(threadTotal);
        final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);
        for (int i = 0; i < clientTotal; i++) {
             final int index = i;
             executorService.execute(() -> {
                try {
                    semaphore.acquire();
                    putValue(index);
                    semaphore.release();
                } catch (InterruptedException e) {
                    log.error("exception", e);
                }
                countDownLatch.countDown();
            });

        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        executorService.shutdown();
        log.info("count:{}", safeMap.size());

      //  testVector();
        testVector1();
    }

    private static void putValue(int i) {
        safeMap.put(i,i);
    }



    public  static  void  testVector(){

        //ExecutorService executorService = Executors.newCachedThreadPool();
        //在这种情况下 线程的 vector 也会变得线程不安全
        while(true) {
            new Thread(() -> {
                for (int i = 0, l = vector.size(); i < l; i++) {
                    vector.remove(i);
                }
            }).start();

            new Thread(() -> {
                for (int i = 0, l = vector.size(); i < l; i++) {
                    vector.get(i);
                }
            }).start();
        }
    }

    public  static  void  testVector1(){
        try {
            for (Integer i : vector) {    //不推荐
               if(i == 3){
                   vector.remove(i);
               }
            }
        }catch (Exception e){
            log.error("foreach循环删除时报错",e);
        }

        try {
            Iterator<Integer> iterator = vector.iterator();
            while(iterator.hasNext()){       //推荐
                  if(iterator.next() == 3){
                      iterator.remove();
                  }
            }
        }catch (Exception e){
            log.error("iterator循环删除时报错",e);
        }


        try {
            for (int i =0; i< vector.size();i++) {  //推荐
                if(i == 3){
                    vector.remove(i);
                }
            }
        }catch (Exception e){
            log.error("for循环删除时报错",e);
        }
    }
}
